package com.xuexi.bootstrap.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/uploadFile")
public class UploadController {

    @RequestMapping("/upload")
    @CrossOrigin
    public JSONObject uploadFile(HttpServletRequest request) throws IOException {
        Map<String, MultipartFile> fileMap = ((MultipartHttpServletRequest) request).getFileMap();
        String filePath="D:/myfile/myzip1/";
        File filePathFile = new File(filePath);
        if (!filePathFile.exists()) {
            filePathFile.mkdirs();// 新建文件夹
        }
        JSONArray filsJsonArray = new JSONArray();
        for (String key : fileMap.keySet()) {
            MultipartFile file = fileMap.get(key);
            if (!file.isEmpty()) {
                try {
                    String fileName = file.getOriginalFilename();
                    fileName = UUID.randomUUID().toString() + "." + getFileSuffix(fileName);
                    File dest = new File(filePath + "/" + fileName);
                    FileUtils.copyInputStreamToFile(file.getInputStream(), dest);
                    JSONObject filJson = new JSONObject();
                    filJson.put("file_name", fileName);
                    filsJsonArray.add(filJson);
                    System.out.println("上传文件成功");
                } catch (Exception e) {
                    System.out.println("文件: " + key + " 上传失败 ==>");
                    return getErrorResult( "文件: " + key + " 上传失败 ==> "
                            + e.getMessage());
                }
            } else {
                System.out.println("文件: " + key
                        + " 上传失败因为文件为空");
                return getErrorResult("文件: " + key
                        + " 上传失败因为文件为空");
            }
        }
        JSONObject result = getSuccResult();
        result.put("file_list", filsJsonArray);
        return result;
    }

    private String getFileSuffix(String fileName) {

        int idx = fileName.lastIndexOf(".");
        if( idx == -1 ) {
            return "";
        }
        return fileName.substring(idx+1);
    }

    private JSONObject getSuccResult() {
        JSONObject gda = new JSONObject(), result = new JSONObject();
        gda.put("msg_typ", "N");
        gda.put("msg_cd", "SCM00000");
        result.put("gda", gda);
        return result;
    }

    private JSONObject getErrorResult(String msgInf) {
        JSONObject gda = new JSONObject(), result = new JSONObject();
        gda.put("msg_typ", "E");
        gda.put("msg_cd", "SCM00010");
        gda.put("msg_inf", msgInf);
        result.put("gda", gda);
        return result;
    }
}